package p.ithorns.framework.redis.stream.mapper;

import org.springframework.data.redis.hash.HashMapper;
import org.springframework.data.redis.serializer.RedisSerializer;
import p.ithorns.framework.redis.stream.serializer.SerializeKey;

import java.util.Collections;
import java.util.Map;

/**
 * MyObjectHashMapper
 * <p>
 * 使用RedisSerializer自定义HashMapper
 *
 * @author ithorns
 * @version 1.0.0
 * @date 2022/12/10 13:25
 */
public class RedisObjectHashMapper<T> implements HashMapper<T, String, T> {


    private final RedisSerializer<T> serializer;


    public RedisObjectHashMapper(RedisSerializer<T> serializer) {
        this.serializer = serializer;
    }

    @Override
    public Map<String, T> toHash(T object) {
        return Collections.singletonMap(SerializeKey.KEY, object);
    }

    @Override
    public T fromHash(Map<String, T> hash) {
        return hash.get(SerializeKey.KEY);
    }

}